package com.gdteam.kernel.event.impl;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

import com.gdteam.kernel.event.Event;
import com.gdteam.kernel.event.EventManager;
import com.gdteam.kernel.main.ServiceManager;

@Aspect
public class EventAspect {
    
    @Pointcut("call(void com.gdteam.kernel.event.Event.fire())")
    public void detectEvent() {
        System.out.println("Event detected");
    }
    
    @Around("detectEvent()")
    public void fireEvent(ProceedingJoinPoint thisJointPoint) {
            Event e = (Event)thisJointPoint.getTarget();
            ((EventManager)((ServiceManager)e.getContext().get(ServiceManager.FQN)).getService(EventManager.FQN)).broadcast(e);
        try {
            thisJointPoint.proceed();
        } catch (Throwable e1) {
            e1.printStackTrace();
        }
    }
    
}
